package com.example.benefitcommunity.service.impl;

import com.example.benefitcommunity.entity.BcMerchant;
import com.example.benefitcommunity.entity.BcMerchantDetail;
import com.example.benefitcommunity.dao.BcMerchantDetailDao;
import com.example.benefitcommunity.entity.Result;
import com.example.benefitcommunity.enums.StatusCode;
import com.example.benefitcommunity.service.BcMerchantDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商户管理明细明细表(BcMerchantDetail)表服务实现类
 *
 * @author makejava
 * @since 2025-08-21 14:48:42
 */
@Transactional(rollbackFor = Exception.class)
@Service("bcMerchantDetailService")
public class BcMerchantDetailServiceImpl implements BcMerchantDetailService {
    @Resource
    private BcMerchantDetailDao bcMerchantDetailDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BcMerchantDetail queryById(Integer id) {
        return this.bcMerchantDetailDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param bcMerchantDetail 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<BcMerchantDetail> queryByPage(BcMerchantDetail bcMerchantDetail, PageRequest pageRequest) {
        long total = this.bcMerchantDetailDao.count(bcMerchantDetail);
        return new PageImpl<>(this.bcMerchantDetailDao.queryAllByLimit(bcMerchantDetail, pageRequest), pageRequest, total);
    }

    /**
     * @param goodname   姓名
     * @param merchantid
     * @param pagenum    页码
     * @param pagesize   页大小
     * @return
     */
    @Override
    public Result<Object> queryByMerchantDetail(String type, String goodname, Integer isupdown, Integer merchantid, Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum, pagesize);
        BcMerchantDetail bcMerchantDetail = new BcMerchantDetail();
        bcMerchantDetail.setType(type);
        bcMerchantDetail.setGoodname(goodname);
        bcMerchantDetail.setIsupdown(isupdown);
        bcMerchantDetail.setMerchantid(merchantid);
        List<BcMerchantDetail> list = this.bcMerchantDetailDao.queryAll(bcMerchantDetail);
        PageInfo<BcMerchantDetail> pageInfo = new PageInfo<>(list);
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增数据
     *
     * @param bcMerchantDetail 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Object> insert(BcMerchantDetail bcMerchantDetail) {
        List<BcMerchantDetail> list = this.bcMerchantDetailDao.queryAll(bcMerchantDetail);
        if (list.isEmpty()) {
            if (this.bcMerchantDetailDao.insert(bcMerchantDetail) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "商户管理明细新增成功！");
            }
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "商户管理明细新增失败！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "商户管理明细已存在！");
    }

    /**
     * 修改数据
     *
     * @param bcMerchantDetail 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Object> update(BcMerchantDetail bcMerchantDetail) {
        BcMerchantDetail bcMerchantDetailNew = this.bcMerchantDetailDao.queryById(bcMerchantDetail.getId());
        if (bcMerchantDetailNew != null) {
            if (this.bcMerchantDetailDao.update(bcMerchantDetail) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "商户管理明细更新成功！");
            }
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "商户管理明细更新失败！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "商户管理明细不存在！");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result<Object> deleteById(Integer[] id) {
        int count = 0;
        //删除个别
        for (Integer i : id) {
            //删除单个
            if (this.bcMerchantDetailDao.deleteById(i) > 0) {
                count++;
            }
        }
        //判断删除结果
        if (count == id.length) {
            return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), (long) id.length, "商户管理明细删除成功！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "商户管理明细删除失败！");
    }
}
